# -*- coding: utf-8 -*-
"""
Created on Wed Dec 26 00:56:13 2012

@author: Winand
"""

import templates
from webapi.log import debug, error
from settings import TEMPLATEFIELDS
#from cgi import escape

def redirResponse(resp, url):
    debug("redir to", url)
    resp.code = 301
    resp.headers['Location'] = url
    resp.responded = True
    
def htmlResponse(resp, html):
    debug("html answer for", resp.path)
    resp.code = 200
    resp.headers['Content-Type'] = "text/html"
    resp.text = html
    resp.responded = True
    
def templateResponse(resp, name, fields={}, ajaxblock=None):
    allfields = dict(TEMPLATEFIELDS.items() + fields.items())
    #convert int values to str
    for field, value in allfields.items():
        if type(value) is int: allfields[field]=str(value) #convert ints to string
#        if type(value) is str: #escape string values to prevent XSS
#            allfields[field]=escape(value)
#        #templates module also escapes string list values
    htmlResponse(resp, templates.template(name, allfields, ajaxblock))